﻿using System;
class SelectionSort
{
    static void Main()
    {
        int lenght;
        int minElement = 1;
        int minIndex = 0;
        Console.Write("please enter the lenght of array: ");
        while (true)
        {
            try
            {
                lenght = int.Parse(Console.ReadLine());
                break;
            }
            catch (Exception)
            {
                Console.Write("Please re-enter lenght:");
            }
        }
        int[] theArray;
        theArray = new int[lenght];
        for (int i = 0; i < lenght; i++)
        {
            Console.Write("Please enter the {0} element of array: ", i);
            while (true)
            {
                try
                {
                    theArray[i] = int.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("Please re-enter element:");
                }
            }
        }
        for (int i = 0; i < lenght; i++)
        {
            minElement = theArray[i];
            for (int j = i; j < lenght; j++)
            {
                if (theArray[j] < minElement)
                {
                    minElement = theArray[j];
                    minIndex = j;
                }
            }
            if (theArray[i] != minElement)
            {
                theArray[i] = (theArray[minIndex] + theArray[i]) - (theArray[minIndex] = theArray[i]);
            }
        }
        for (int k = 0; k < lenght; k++)
        {
            Console.WriteLine(theArray[k]);
        }
        Console.ReadLine();
    }
}

